/*
 * Copyright [yyyy] [name of copyright owner]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * You may obtain a copy of the License at

 * http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flarebit.flarebar.slice;

import com.flarebit.flarebar.ResourceTable;
import com.flarebit.flarebarlib.FlareBar;
import com.flarebit.flarebarlib.FlareUtils;
import com.flarebit.flarebarlib.Flaretab;
import com.flarebit.flarebarlib.TabEventObject;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.utils.Color;
import ohos.bundle.AbilityInfo;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.media.image.PixelMap;

import java.util.ArrayList;

/**
 * MainAbilitySlice
 *
 * @author Vaibhav Patel
 * @since 2019-06-20
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final int DELAYMS = 2000;
    Button btnCheck;
    FlareBar bottomBar;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        getWindow().setStatusBarColor(Color.getIntColor("#303F9F"));
        bottomBar = (FlareBar) findComponentById(ResourceTable.Id_bottomBar);
        btnCheck = (Button) findComponentById(ResourceTable.Id_check);
        bottomBar.setBarBackgroundColor(Color.getIntColor("#FFFFFFFF"));
        ArrayList<Flaretab> tabs = new ArrayList<>();

        tabs.add(new Flaretab(getElement(ResourceTable.Media_inboxb), "Inbox", "#FFFFECB3"));
        tabs.add(new Flaretab(getElement(ResourceTable.Media_searchb), "Search", "#FF80DEEA"));
        tabs.add(new Flaretab(getElement(ResourceTable.Media_phoneb), "Call Log", "#FFB39DDB"));
        tabs.add(new Flaretab(getElement(ResourceTable.Media_avatarb), "Profile", "#FFEF9A9A", "4"));
        tabs.add(new Flaretab(getElement(ResourceTable.Media_settingsb), "Settings", "#FFB2DFDB"));

        bottomBar.setTabList(tabs);
        bottomBar.attachTabs(getContext());
        Text text = (Text) findComponentById(ResourceTable.Id_text_msg_toast);
        EventHandler handler = new EventHandler(EventRunner.getMainEventRunner());
        bottomBar.setTabChangedListener(new TabEventObject.TabChangedListener() {
            @Override
            public void onTabChanged(DirectionalLayout selectedTab, int selectedIndex, int oldIndex) {
                text.setVisibility(Component.VISIBLE);
                text.setText("Tab " + selectedIndex + " Selected.");
                handler.postTask(new Runnable() {
                    @Override
                    public void run() {
                        text.setVisibility(Component.HIDE);
                    }
                }, DELAYMS);
            }
        });

        btnCheck.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (bottomBar.isBarVisible()) {
                    bottomBar.hideBar();
                } else {
                    bottomBar.showBar();
                }
            }
        });
    }

    private Element getElement(int id) {
        PixelMap pixelMap = FlareUtils.getPixelMapFromResource(getContext(), id);
        return new PixelMapElement(pixelMap);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
